const express = require('express');
const { body, param, query, validationResult } = require('express-validator');
const { authMiddleware, optionalAuthMiddleware, adminMiddleware } = require('../middlewares/auth.middleware'); // 引入 adminMiddleware
const { validateRequest } = require('../middlewares/validation.middleware'); // <--- 导入 validateRequest
const commentController = require('../controllers/comment.controller');

const router = express.Router();

// --- Validation Chains ---
const validateTarget = [
    param('targetType').isIn(['video', 'post']).withMessage('无效的目标类型'),
    param('targetId').isMongoId().withMessage('无效的目标ID')
];

const validateCommentContent = [
    body('text').trim().isLength({ min: 1, max: 1000 }).withMessage('评论内容长度必须在1-1000个字符之间')
];

const validatePagination = [
    query('page').optional().isInt({ min: 1 }).toInt(),
    query('limit').optional().isInt({ min: 1, max: 50 }).toInt(),
    query('sort').optional().isIn(['latest', 'popular'])
];

// --- Routes ---

// --- Admin Routes ---
// 新增：管理员获取所有评论列表 (支持分页、搜索、过滤)
router.get('/admin', [
    authMiddleware, // <--- 添加 authMiddleware
    adminMiddleware, // 确保只有管理员可以访问
    query('page').optional().isInt({ min: 1 }).toInt(),
    query('limit').optional().isInt({ min: 1, max: 100 }).toInt(),
    query('search').optional({ checkFalsy: true }).trim(), // 允许空字符串
    query('type').optional({ checkFalsy: true }).isIn(['video', 'post']), // 允许空字符串, null, undefined
    query('status').optional({ checkFalsy: true }).isIn(['normal', 'hidden']), // 允许空字符串, null, undefined
    validateRequest, // 应用验证结果
], commentController.getAllCommentsForAdmin);

// 批量更新评论状态 (显示/隐藏)
router.patch('/admin/status', [
    adminMiddleware,
    body('ids').isArray({ min: 1 }).withMessage('请提供评论ID列表'),
    body('ids.*').isMongoId().withMessage('无效的评论ID'),
    body('status').isIn(['normal', 'hidden']).withMessage('无效的状态')
], commentController.batchUpdateCommentStatus);

// 新增：管理员批量删除评论
router.delete('/admin', [
    adminMiddleware,
    body('ids').isArray({ min: 1 }).withMessage('请提供评论ID列表'),
    body('ids.*').isMongoId().withMessage('无效的评论ID')
], commentController.batchDeleteComments);

// --- Public/User Routes ---

// 添加评论 (视频或帖子)
router.post('/', [
    authMiddleware,
    body('targetType').isIn(['video', 'post']).withMessage('无效的目标类型'),
    body('targetId').isMongoId().withMessage('无效的目标ID'),
    body('text').trim().isLength({ min: 1, max: 1000 }).withMessage('评论内容长度必须在1-1000个字符之间'),
    body('parentId').optional().isMongoId().withMessage('无效的父评论ID')
], commentController.addComment);

// 获取评论列表 (视频或帖子)
router.get('/target/:targetType/:targetId', [
    ...validateTarget,
    ...validatePagination
], commentController.getComments);

// 更新评论
router.patch('/:id', [
    authMiddleware,
    param('id').isMongoId().withMessage('无效的评论ID'),
    ...validateCommentContent
], commentController.updateComment);

// 删除评论
router.delete('/:id', [
    authMiddleware,
    param('id').isMongoId().withMessage('无效的评论ID')
], commentController.deleteComment);

// 点赞/取消点赞评论
router.post('/:id/like', [
    authMiddleware,
    param('id').isMongoId().withMessage('无效的评论ID')
], commentController.likeComment);

// 获取用户对评论的交互状态 (是否点赞)
router.get('/:id/interaction', [
    optionalAuthMiddleware, // 允许未登录用户查看，但交互状态只对登录用户有效
    param('id').isMongoId().withMessage('无效的评论ID')
], commentController.getCommentInteractionStatus);


module.exports = router;

/*
 * Removed old video-specific routes:
 * - GET /video/:videoId
 * - POST /video/:videoId
 * - GET /:commentId/replies (Functionality merged into getComments with level filter if needed)
 * - PATCH /:commentId (Replaced with PATCH /:id)
 * - DELETE /:commentId (Replaced with DELETE /:id)
 */